<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
 
namespace osc\mobile\controller;
use osc\common\controller\Base;
use \think\Db;
use wechat\Curl;

//团购商品支付类
class TuanPayment extends Base{
	
	function alipay_pay(){
		if(request()->isPost()){
			
			$post=input('param.');
			
			$order=osc_service('common','Tuan')->add_order($post['id'],$post['address_id'],'alipay',$post['comment']);
				
			if(isset($order['error'])){
				return array('error'=>$order['error']);
			}	
						
			$config=payment_config('alipay');
			
			$alipay_config = array(
					"service"       => 'alipay.wap.create.direct.pay.by.user',
					"partner"       => $config['partner'],
					"seller_id"     => $config['partner'],
					"key"			=> $config['key'],
					"payment_type"	=> 1,
					"notify_url"	=> request()->domain().url('payment/alipay_notify'),
					"return_url"	=> request()->domain().url('payment/alipay_return'),
					"_input_charset"	=> trim(strtolower(strtolower('utf-8'))),
					"out_trade_no"	=> $order['pay_order_no'],
					"subject"	=> $order['subject'],
					"total_fee"	=> $order['pay_total'],
					"show_url"	=> '',
					'transport'=>'http',
					'sign_type'=>strtoupper('MD5'),
					//"app_pay"	=> "Y",//启用此参数能唤起钱包APP支付宝
					"body"	=> '',								
			);	
			
			$alipay = new \payment\alipay\Alipay($alipay_config,'mobile');
			
			$url = $alipay->get_payurl();
			
			osc_service('common','Tuan')->clear_cart($order['uid']);	
			
			return ['success'=>1,'url'=>$url];
			
		}
	}
	
	function predeposit_pay(){
		
		$post=input('param.');		
			
		$order=osc_service('common','Tuan')->add_order($post['id'],$post['address_id'],'predeposit',$post['comment']);
		
		if(isset($order['error'])){
			return array('error'=>$order['error']);
		}else{
			
			osc_service('common','Tuan')->clear_cart($order['uid']);			
			osc_order()->update_order($order['order_id']);			
			osc_service('common','Predeposit')->update_predeposit($order);		
				
			return array('success'=>'支付成功');
		}
	}
	
	function weixin_pay(){
		
		$order=session('jssdk_tuan_order');
		
		if(empty($order)){
		
			$post=input('param.');		
				
			$order=osc_service('common','Tuan')->add_order($post['id'],$post['address_id'],'weixin',$post['comment']);
			
			if(isset($order['error'])){
				return array('error'=>$order['error']);
			}
			
			session('jssdk_tuan_order',$order);
		}
		
		$pay=new \osc\mobile\controller\Payment();
		
		$param['pay_total']=$order['total'];
		$param['subject']=$order['subject'];	
		$param['pay_order_no']=$order['pay_order_no'];	
		$param['notify_url']=request()->domain().url('TuanPayment/jsskd_notify');
		
		return $pay->getBizPackage($param);
		
	}
	//微信jssdk回调
	public function jsskd_notify(){
		
		if(wechat()->checkPaySign()){
			
			$sourceStr = file_get_contents('php://input');		
		 
	        // 读取数据
	        $postObj = simplexml_load_string($sourceStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		 
			if (!$postObj) {
	       		 echo "<xml><return_code><![CDATA[FAIL]]></return_code></xml>";
	        } else {
			
				$order=Db::name('order')->where('order_num_alias',$postObj->out_trade_no)->find();		
					
				if($order&&($order['order_status_id']!=config('paid_order_status_id'))){
										
					osc_order()->update_order($order['order_id']);
					
					osc_service('common','Tuan')->update_buyer_count($order['group_buy_id']);
					
					echo "<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>";	
									
				}else{
					echo "<xml><return_code><![CDATA[FAIL]]></return_code></xml>";
				}				
	        }			
		}else{
			echo "<xml><return_code><![CDATA[FAIL]]></return_code></xml>";
		}
		die;
	}
}